<?php

class D_Mail {
	
	static function send_by_template($mailtemplate,$params,$subject,$to = '', $from = '', $reply = ''){
			$view=new Zend_View();
	    	$view->setScriptPath("./application/mailtemplates");
	    	$view->params=$params;
	    	$body=$view->render($mailtemplate);
	    	return D_Mail::send($subject, $body, $to, $from, $reply);
	}
	
	static function send( $subject, $body, $to = '', $from = '', $reply = '') {
		//$subject = mb_convert_encoding($subject, 'ISO-2022-JP', 'UTF-8');
		//$body = mb_convert_encoding($body, 'ISO-2022-JP', 'UTF-8');
		
		$config_mail = new Zend_Config_Ini ( './application/configs/application.ini', 'mail_config' );
		
		$from = $from == '' ? $config_mail->from : $from;
		$to = $to == '' ? $config_mail->to : $to;
		$reply = $reply == '' ? $config_mail->reply : $reply;
		
		$mail = new Zend_Mail('UTF-8', 'UTF-8');
		$mail->addHeader('Content-Type', 'text/plain;charset=UTF-8');
		$mail->setHeaderEncoding(Zend_Mime::ENCODING_BASE64);
		$mail->setBodyText($body,"UTF-8", Zend_Mime::ENCODING_8BIT);
		$mail->setFrom ( $from );
		$mail->setReplyTo ( $reply );
		$mail->setSubject ( $subject );
		$mail->addTo($to);
		/*Mail*/
		if ($mail->send ()) {
			return true;
		}
		return false;
	}
	
	
	//use from batch
	static function send_by_template_by_batch($mailtemplate,$params,$subject,$to = '', $from = '', $reply = ''){
			$view=new Zend_View();
	    	$view->setScriptPath(ROOT_PATH."/application/mailtemplates");
	    	$view->params=$params;
	    	$body=$view->render($mailtemplate);
	    	return D_Mail::sendByBatch($subject, $body, $to, $from, $reply);
	}
	
	static function sendByBatch( $subject, $body, $to = '', $from = '', $reply = '') {
		$config_mail = new Zend_Config_Ini ( ROOT_PATH.'/application/configs/application.ini', 'mail_config' );
		
		$from = $from == '' ? $config_mail->from : $from;
		$to = $to == '' ? $config_mail->to : $to;
		$reply = $reply == '' ? $config_mail->reply : $reply;
		$mail = new Zend_Mail ( 'UTF-8' );
		
		$mail = new Zend_Mail('UTF-8', 'UTF-8');
		$mail->addHeader('Content-Type', 'text/plain;charset=UTF-8');
		$mail->setHeaderEncoding(Zend_Mime::ENCODING_BASE64);
		$mail->setBodyText($body,"UTF-8", Zend_Mime::ENCODING_8BIT);
		$mail->setFrom ( $from );
		$mail->setReplyTo ( $reply );
		$mail->setSubject ( $subject );
		$mail->addTo($to);
		/*Mail*/
		if ($mail->send ()) {
			return true;
		}
		return false;
	}
	
	
}

?>